package GUI;

import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;

import ControlLayer.PartOrderCtr;
import ModelLayer.PartOrder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PartOrderList extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4050104982918137182L;
	private JPanel contentPane;
	private JList<PartOrder> lstPart;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PartOrderList frame = new PartOrderList();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PartOrderList() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 549, 370);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JButton btnVisAlle = new JButton("vis alle ");
		btnVisAlle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updatePartOrderList();
			}
		});
		btnVisAlle.setBounds(434, 298, 97, 25);
		contentPane.add(btnVisAlle);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 531, 297);
		contentPane.add(scrollPane);
		
		JList lstPart = new JList<Object>();
		scrollPane.setViewportView(lstPart);
	}
	
	private void updatePartOrderList()
	{
		ArrayList<PartOrder> list = new PartOrderCtr().findAllPartOrder();
		DefaultListModel<PartOrder> dlist = new DefaultListModel<>();
		for(PartOrder order: list)
			dlist.addElement(order);
		lstPart.setModel(dlist);
	}

}
